﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace BBMS.DLL.DataContract
{
    public class Log : BBMS.DLL.Base.ObjectBase
    {

        public Int32 ID;
        public String Username;
        public Int16 Action;    
        public DateTime LogDate;
        public String Message;
        public String Table;
        public String ComputerIP;



        public Log()
        {
        }

        public Log(Int32 id, String username, Int16 action, DateTime logdate, String message, String table, String computerip)
        {
            this.ID = id;
            //this.Key = this.ID; // Key of object base is read-only, do not change this
            this.Username = username;
            this.Action = action;
            this.LogDate = logdate;
            this.Message = message;
            this.Table = table;
            this.ComputerIP = computerip;

        }

        public override object Key
        {
            get
            {
                return this.ID;
            }
        }
       
        public override string TableName
        {
            get
            {
                return "Logs";
            }
        }

        public override string[] KeyFields
        {
            get
            {
                return new string[] { "ID" };
            }
        }

        public override string[] IDFields
        {
            get
            {
                return new string[] { "ID" };
            }
        }
        public override bool CanDelete(MySql.Data.MySqlClient.MySqlCommand cmd, System.Data.DataRow row)
        {
            bool ret = false;
            try
            {
                //return base.CanDelete(cmd, row);
                cmd.CommandText = "select count(ID) from logs where ID = @ID";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ID", this.ID);
                long count = (long)cmd.ExecuteScalar();
                if (count == 0)
                {
                    ret = true;
                }
                cmd.Parameters.Clear();
            }
            catch { }
            return ret;
        }

        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(this.TableName);
            dt.Columns.Add("ID", typeof(Int32));
            dt.Columns.Add("Username", typeof(String));
            dt.Columns.Add("Action", typeof(String));
            dt.Columns.Add("LogDate", typeof(DateTime));
            dt.Columns.Add("Message", typeof(String));
            dt.Columns.Add("TableName", typeof(String));
            dt.Columns.Add("ComputerIP", typeof(String));
            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["ID"] = this.ID;
            row["Username"] = this.Username;
            row["Action"] = this.Action;
            row["LogDate"] = this.LogDate;
            row["Message"] = this.Message;
            row["TableName"] = this.Table;
            row["ComputerIP"] = this.ComputerIP;

            return row;
        }

        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            Log obj = null;
            try
            {
                obj = new Log(Convert.ToInt32(row["ID"].ToString()), row["UserName"].ToString(),
                 Convert.ToInt16(row["Action"].ToString()), Convert.ToDateTime(row["LogDate"].ToString()),
                 row["Message"].ToString(), row["TableName"].ToString(), row["ComputerIP"].ToString());
            
            }
            catch { }
            return obj;
        }

        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader rd)
        {
            Log obj = null;
            try
            {
                obj = new Log(Convert.ToInt32(rd["ID"].ToString()), rd["UserName"].ToString(),
                     Convert.ToInt16(rd["Action"].ToString()), Convert.ToDateTime(rd["LogDate"].ToString()),
                     rd["Message"].ToString(), rd["TableName"].ToString(), rd["ComputerIP"].ToString());
            }
            catch { }
            return obj;
        }

     


        //public override string InsertNew()
        //{
        //    string msg = "";
        //    try
        //    {
        //        MySqlConnection con = new MySqlConnection(Base.Config.ConnectionString());
        //        con.Open();
        //        MySqlTransaction tran = con.BeginTransaction();
        //        try
        //        {
        //            MySqlCommand cmd = con.CreateCommand();

        //            cmd.CommandText = "insert into logs " +
        //                " (Username, Action, NgayLog, Message, Category)" +
        //                "values (@Username, @Action, @NgayLog, @Message, @Category) ";
        //            cmd.Parameters.AddWithValue("@Username", MySqlDbType.String);
        //            cmd.Parameters.AddWithValue("@Action", MySqlDbType.Int16);
        //            cmd.Parameters.AddWithValue("@NgayLog", MySqlDbType.DateTime);
        //            cmd.Parameters.AddWithValue("@Message", MySqlDbType.String);
        //            cmd.Parameters.AddWithValue("@Category", MySqlDbType.Int16);
        //            cmd.Parameters["@Username"].Value = this.Username;
        //            cmd.Parameters["@Action"].Value = this.Action;
        //            cmd.Parameters["@NgayLog"].Value = this.NgayLog;
        //            cmd.Parameters["@Message"].Value = this.Message;
        //            cmd.Parameters["@Category"].Value = this.Category;
        //            cmd.ExecuteNonQuery();
        //            cmd.CommandText = "SELECT LAST_INSERT_ID()";
        //            this.ID = (int)cmd.ExecuteScalar();
        //            if (this.ID > 0)
        //                tran.Commit();
        //            else tran.Rollback();
        //        }
        //        catch
        //        {
        //            tran.Rollback();
        //            msg = "Lỗi khi chèn mới bản ghi.";
        //        }
        //        tran.Dispose();
        //        con.Close();
        //        con.Dispose();
        //    }
        //    catch { msg = "Lỗi khi kết nối đến cơ sở dữ liệu."; }
        //    return msg;
        //}

     




        //public override string InsertNew()
        //{
        //    string msg = "";
        //    try
        //    {
        //        MySqlConnection con = new MySqlConnection(Base.Config.ConnectionString());
        //        con.Open();
        //        MySqlTransaction tran = con.BeginTransaction();
        //        try
        //        {
        //            MySqlCommand cmd = con.CreateCommand();

        //            cmd.CommandText = "insert into logs " +
        //                " (Username, Action, NgayLog, Message, Category)" +
        //                "values (@Username, @Action, @NgayLog, @Message, @Category) ";
        //            cmd.Parameters.AddWithValue("@Username", MySqlDbType.String);
        //            cmd.Parameters.AddWithValue("@Action", MySqlDbType.Int16);
        //            cmd.Parameters.AddWithValue("@NgayLog", MySqlDbType.DateTime);
        //            cmd.Parameters.AddWithValue("@Message", MySqlDbType.String);
        //            cmd.Parameters.AddWithValue("@Category", MySqlDbType.Int16);
        //            cmd.Parameters["@Username"].Value = this.Username;
        //            cmd.Parameters["@Action"].Value = this.Action;
        //            cmd.Parameters["@NgayLog"].Value = this.NgayLog;
        //            cmd.Parameters["@Message"].Value = this.Message;
        //            cmd.Parameters["@Category"].Value = this.Category;
        //            cmd.ExecuteNonQuery();
        //            cmd.CommandText = "SELECT LAST_INSERT_ID()";
        //            this.ID = (int)cmd.ExecuteScalar();
        //            if (this.ID > 0)
        //                tran.Commit();
        //            else tran.Rollback();
        //        }
        //        catch
        //        {
        //            tran.Rollback();
        //            msg = "Lỗi khi chèn mới bản ghi.";
        //        }
        //        tran.Dispose();
        //        con.Close();
        //        con.Dispose();
        //    }
        //    catch { msg = "Lỗi khi kết nối đến cơ sở dữ liệu."; }
        //    return msg;
        //}

        ////public Int32 ID;
        ////public String Username;
        ////public Int16 Action;
        ////public String ActionString = "";
        ////public DateTime LogDate;
        ////public String Message = "";
        ////public String TableName = "";
        ////public String ComputerIP = "";



        ////public Log()
        ////{
        ////}

        ////public Log(Int32 id, String username, Int16 action, DateTime ngaylog, String message, Int16 category)
        ////{
        ////    this.ID = id;
        ////    //this.Key = this.ID; // Key of object base is read-only, do not change this
        ////    this.Username = username;
        ////    this.Action = action;
        ////    this.NgayLog = ngaylog;
        ////    this.Message = message;
        ////    this.Category = category;

        ////}

        ////public Log(Int32 id, String username, Int16 action, String actionstring, DateTime ngaylog, String message, Int16 category, String categorystring)
        ////{
        ////    this.ID = id;
        ////    //this.Key = this.ID; // Key of object base is read-only, do not change this
        ////    this.Username = username;
        ////    this.Action = action;
        ////    this.ActionString = actionstring;
        ////    this.NgayLog = ngaylog;
        ////    this.Message = message;
        ////    this.Category = category;
        ////    this.CategoryString = categorystring;
        ////}



        
     

        ////public override string InsertNew()
        ////{
        ////    string msg = "";
        ////    try
        ////    {
        ////        MySqlConnection con = new MySqlConnection(Base.Config.ConnectionString());
        ////        con.Open();
        ////        MySqlTransaction tran = con.BeginTransaction();
        ////        try
        ////        {
        ////            MySqlCommand cmd = con.CreateCommand();

        ////            cmd.CommandText = "insert into logs " +
        ////                " (Username, Action, NgayLog, Message, Category)" +
        ////                "values (@Username, @Action, @NgayLog, @Message, @Category) ";
        ////            cmd.Parameters.AddWithValue("@Username", MySqlDbType.String);
        ////            cmd.Parameters.AddWithValue("@Action", MySqlDbType.Int16);
        ////            cmd.Parameters.AddWithValue("@NgayLog", MySqlDbType.DateTime);
        ////            cmd.Parameters.AddWithValue("@Message", MySqlDbType.String);
        ////            cmd.Parameters.AddWithValue("@Category", MySqlDbType.Int16);
        ////            cmd.Parameters["@Username"].Value = this.Username;
        ////            cmd.Parameters["@Action"].Value = this.Action;
        ////            cmd.Parameters["@NgayLog"].Value = this.NgayLog;
        ////            cmd.Parameters["@Message"].Value = this.Message;
        ////            cmd.Parameters["@Category"].Value = this.Category;
        ////            cmd.ExecuteNonQuery();
        ////            cmd.CommandText = "SELECT LAST_INSERT_ID()";
        ////            this.ID = (int)cmd.ExecuteScalar();
        ////            if (this.ID > 0)
        ////                tran.Commit();
        ////            else tran.Rollback();
        ////        }
        ////        catch
        ////        {
        ////            tran.Rollback();
        ////            msg = "Lỗi khi chèn mới bản ghi.";
        ////        }
        ////        tran.Dispose();
        ////        con.Close();
        ////        con.Dispose();
        ////    }
        ////    catch { msg = "Lỗi khi kết nối đến cơ sở dữ liệu."; }
        ////    return msg;
        ////}
    }
}
